package interfaces;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

import model.AlarmClockWrapper;
import model.AlarmTypeWrapper;
import model.EditType;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import services.User;
import services.UserDataServicesProxy;

import com.toedter.calendar.JDateChooser;

public class AlarmClockEditorDialog  extends JPanel {
	private static final long serialVersionUID = 1L;
	private JComboBox<AlarmTypeWrapper> box = new JComboBox<AlarmTypeWrapper>();
	private JDateChooser start = new JDateChooser();
	private JDateChooser end = new JDateChooser();
	private DaysPanel daysPanel = new DaysPanel();
	private JSpinner startTimeSpinner;
	private JButton okButton = new JButton("Ок");
	private JButton cancelButton = new JButton("Отмена");
	private JDialog dialog;
	private AlarmClockWrapper result;
	private User user;
	private EditType type;
	public AlarmClockEditorDialog(JDialog dialog, User user, AlarmClockWrapper data, EditType type, List<AlarmTypeWrapper> atw) {
		super();
		this.dialog = dialog;
		result = data;
		this.user = user;
		this.type = type;
		initComponents(atw);
		initListeners();
		initValues();
	}
	
	
	private void initValues() {
		start.setDate(result.getStartDate().toDate());
		end.setDate(result.getEndDate().toDate());
		startTimeSpinner.setValue(result.getTime().toDate());
		box.setSelectedItem(result.getType());
	}

	public void pack() {
		result.setUserid(user.getId());
		Date date = start.getDate();
		if(date != null)
			result.setStartDate(new LocalDate(date.getTime()));
		date = end.getDate();
		if(date != null)
			result.setEndDate(new LocalDate(date.getTime()));
		result.setTime(new DateTime((Date) startTimeSpinner.getValue()));
		result.setType((AlarmTypeWrapper) box.getSelectedItem());
	}
	private void initListeners() {
		okButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				pack();
				UserDataServicesProxy proxy = new UserDataServicesProxy();
				try {
					switch (type) {
					case ADD:
						int setData = proxy.addAlarmClock(result.toAlarmClock());
						if(setData == -1) {
							JOptionPane.showMessageDialog(null, "Не удалось сохранить данные в БД");
							return;
						} else {
							result.setId(setData);
						}
						break;
					case EDIT:
						proxy.updateAlarmClock(result.toAlarmClock());
						break;
					default:
						break;
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				dialog.dispose();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				result = null;
				dialog.dispose();
			}
		});
	}


	private void initComponents(List<AlarmTypeWrapper> atw2) {
		JPanel northPanel = new JPanel(new GridBagLayout());
		for(AlarmTypeWrapper atw : atw2) {
			box.addItem(atw);
		}
		box.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				AlarmTypeWrapper atw = (AlarmTypeWrapper) e.getItem();
				daysPanel.setEnabled(atw.getType() == 2);
			}
		});
		JPanel datePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		datePanel.add(new JLabel("Первый день"));
		datePanel.add(start);
		datePanel.add(new JLabel("Последний день"));
		datePanel.add(end);
		datePanel.add(new JLabel("Время"));
		SpinnerModel model = new SpinnerDateModel();
		startTimeSpinner = new JSpinner(model);
		JComponent editor = new JSpinner.DateEditor(startTimeSpinner, "HH:mm");
		startTimeSpinner.setEditor(editor);
		datePanel.add(startTimeSpinner);
		northPanel.add(datePanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0));
		
		JPanel dayPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		dayPanel.add(box);
		dayPanel.add(daysPanel);
		northPanel.add(dayPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0));
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		northPanel.add(buttonPanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0));
		add(northPanel);
	}



	public AlarmClockWrapper getResult() {
		return result;
	}

	public static AlarmClockWrapper showDialog(User user, AlarmClockWrapper data, EditType type, List<AlarmTypeWrapper> atw) {
		JDialog dialog = new JDialog();
		AlarmClockEditorDialog authDialog = new AlarmClockEditorDialog(dialog, user, data, type, atw);
		dialog.add(authDialog);
		if(type.equals(EditType.ADD))
			dialog.setTitle("Добаление");
		else
			dialog.setTitle("Редактирование");
		dialog.setModal(true);
		dialog.setLocationRelativeTo(null);
		dialog.pack();
		dialog.setVisible(true);
		return authDialog.getResult();
	}
}
